package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2022-06-24
 * Time: 18:57
 */
public class DelOperation implements IOperation{
    @Override
    public void work(BookList booklist) {
        System.out.println("删除图书");
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入删除图书的名字：");
        String  name= scanner.nextLine();
        int currentSize= booklist.getUsedSize();
        int index=-1;
        int i =0;
        for (; i <currentSize ; i++) {
            Book  book=booklist.getBook(i);
            if(book.getName().equals(name)){
                index=i;
                break;
            }
        }

        if(i>=currentSize){
            System.out.println("没有找到这本书");
            return;
        }
            for(int j=index;j<currentSize ;j++){
                Book book=booklist.getBook(i+1);
                booklist.setBooks(i,book);
            }
            booklist.setBooks( currentSize-1,null);//删除了书籍，进行了移动，然后将最后的置为空，不会有内存泄漏
            booklist.setUsedSize(currentSize-1);
        System.out.println("删除成功");
    }
}
